﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;
using He.Common;

namespace He.Web.manage.member
{
    public partial class rank_edit : Manage
    {

        public string action = Enums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        public string strFunc;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = GetString("action");

            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    ShowParamError();
                    return;
                }
            }
            if (!IsPostBack)
            {
                if (action == Enums.ActionEnum.Edit.ToString()) //修改
                {
                    chkLoginLevel("editmember");
                    ShowInfo();
                }
                else
                {
                    chkLoginLevel("addmember");
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo()
        {
            Model.member_rank model = new BLL.member_rank().GetModel(id);
            txtTitle.Text = model.title;
            txtRemark.Text = model.remark;
            this.strFunc = model.enable_func;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            BLL.member_rank bll = new BLL.member_rank();
            Model.member_rank model = new Model.member_rank();

            model.title = txtTitle.Text.Trim();
            model.enable_func = "," + Request.Form["cbFunc"] + ",";
            model.remark = txtRemark.Text;
            model.is_sys = 0;
            if (bll.Add(model) > 0)
            {
                SaveLogs(string.Format("[会员等级管理]增加会员等级:{0}", model.title));
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            BLL.member_rank bll = new BLL.member_rank();
            Model.member_rank model = bll.GetModel(_id);
            model.title = txtTitle.Text;
            model.enable_func = "," + Request.Form["cbFunc"] + ",";
            model.remark = txtRemark.Text;
            if (bll.Update(model))
            {
                SaveLogs(string.Format("[会员等级管理]修改会员等级:{0}", model.title));
                return true;
            }
            return false;
        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    ShowError("保存过程中发生错误啦！");
                    return;
                }
                ShowSuccess("修改成功啦！", "rank_list.aspx");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    ShowError("保存过程中发生错误啦！");
                    return;
                }
                ShowSuccess("增加成功啦！", "rank_list.aspx");
            }
        }
    }
}